
from inspect import signature as sig

def from_string(msg):
    try:
        import ossl, xml.etree.ElementTree as ET
        doc = ET.ElementTree()
        doc._setroot(ET.fromstring(msg))
        doc.__dict__["parent_map"] = {c: p for p in doc.getroot().iter() for c in p}
        doc.__dict__["parent"] = lambda elem: doc.parent_map.get(elem)
        return doc
    except Exception as e:
        print(e)
        return None

def from_file(name):
    try:
        import ossl, xml.etree.ElementTree as ET
        doc = ET.parse(ossl.__path__[0] + "\\addon_contents.xml")
        doc.__dict__["parent_map"] = {c: p for p in doc.getroot().iter() for c in p}
        doc.__dict__["parent"] = lambda elem: doc.parent_map.get(elem)
        return doc
    except Exception as e:
        print(e)
        return None
    
def next(it):
    try: return it.__next__()
    except: return None

def xxx():
    doc = xml.from_string(msg)    
    elem = doc.find("./dir//file")
    elem = doc.parent(elem) 

    doc_local = xml.from_file(util.path("addon_contents.xml"))
    root = doc_local._root
    for file in doc_local.findall(".//file[@dt='2021-08-13 05:59:16']"):
        print(file.get("name"), file.items())
        p = doc_local.parent(file)
        print(p.tag, p.items())

    root = doc.getroot()
    print("############ ", root.items())
    #print("verifying addon", doc.tag, doc.get("name"), doc.get("dt"), "\n", dir(doc))
    doc_local = ET.parse(ossl.__path__[0] + "\\addon_contents.xml")
    print("############ ", type(doc_local))
#    print("     with addon", dir(doc_local))

    root = doc_local._root

#    print("     with addon", root.tag, root.get("name"), root.get("dt"))

    parent_map = {c: p for p in root.iter() for c in p}

    for file in doc_local.findall(".//file[@dt='2021-08-13 05:59:16']"):
#        print(file.get("name"), file.items(), file.find(".."))
        p = parent_map.get(file)
#        print(p.tag, p.items())

    #print(ET.tostring(root))

if __name__ == "__main__":
    import ossl.util as util
    print("\n")
    util.reset_addon()
